//
//  DrawView.m
//  MJAVFoundationDemo
//
//  Created by apple on 16/10/10.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView
-(id)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor clearColor];
        _selectedArray=[NSMutableArray array];
        [self setUpBtns];
        [self setNeedsDisplay];
    }
    return self;
}
#pragma mark--创建按钮控件
-(void)setUpBtns
{
    CGFloat internal=(KWIDTH-180)/4.0;
    CGFloat internalY=(KHEIGHT-64-180-2*internal)/2;
    for (int i=0; i<9; i++)
    {
        UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(i%3*(internal+60)+internal, i/3*(internal+60)+internalY, 60, 60)];
        bgView.backgroundColor=[UIColor clearColor];
        bgView.tag=(i+1)*10;
        bgView.userInteractionEnabled=YES;
        [self addSubview:bgView];

        
        UIButton *btn=[[UIButton alloc]init];
        btn.bounds=CGRectMake(0, 0, 60, 60);
        btn.center=CGPointMake(30, 30);
        btn.tag=100;
        [btn setBackgroundImage:[UIImage imageNamed:@"no_selected"] forState:UIControlStateNormal];
        //这个属性不设为no的话，当手指第一触碰的是按钮则不会被选中
        btn.userInteractionEnabled=NO;
        [btn setBackgroundImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
        [bgView addSubview:btn];
        
        UIImageView *smallImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"4"]];
        smallImage.tag=200;
        smallImage.userInteractionEnabled=NO;
        smallImage.bounds=CGRectMake(0, 0, 30, 30);
        smallImage.center=CGPointMake(btn.center.x, CGRectGetMaxY(btn.frame)+15);
        smallImage.hidden=YES;
        [bgView addSubview:smallImage];
       
    }
}
#pragma mark--手指第一次触摸到按钮时记录这个点
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point=[self getCurrentPoint:touches];
    //遍历所有按钮找到被手指摸到的那个按钮
    UIButton *btn=[self getSelectedBtn:point];
    //如果按钮没有被选中过就改变它的背景图并且让其被选中
    if (btn && !btn.isSelected)
    {
        btn.selected=YES;
        [_selectedArray addObject:btn.superview];
    }}
#pragma mark--移动到哪个按钮就记录这个按钮
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //获取手指在触摸视图上的坐标位置
    CGPoint point=[self getCurrentPoint:touches];
    //遍历所有按钮找到被手指摸到的那个按钮
    UIButton *btn=[self getSelectedBtn:point];
    //如果按钮没有被选中过就改变它的背景图并且让其被选中
    if (btn && !btn.isSelected)
    {
        btn.selected=YES;
        [_selectedArray addObject:btn.superview];
    }
    //执行这一句就开始画图
    [self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}
#pragma mark--获取手指触摸的坐标点
-(CGPoint)getCurrentPoint:(NSSet *)touches
{
    UITouch *touch=[touches anyObject];
    CGPoint point=[touch locationInView:self];
    return point;
}
#pragma mark--获取被手指摸到的那个按钮
-(UIButton *)getSelectedBtn:(CGPoint)point
{
    NSArray *Arr=self.subviews;
    for (UIView  *view in Arr)
    {
        
        if (CGRectContainsPoint(view.frame, point))
        {
            UIButton *btn=[view viewWithTag:100];
            return btn;
        }
    }
    return nil;
}

#pragma mark--开始在被选中的按钮上划线
- (void)drawRect:(CGRect)rect
{
    //创建画笔
    CGContextRef context=UIGraphicsGetCurrentContext();
    for (int i=0;i<_selectedArray.count;i++)
    {
        UIView * view=[_selectedArray objectAtIndex:i];
        if (i==0) {
            //画笔移到起点
            CGContextMoveToPoint(context, view.center.x, view.center.y);
        }
        else
        {
            //添加线条
            CGContextAddLineToPoint(context, view.center.x, view.center.y);
        }
    }
    //设置线条颜色
    CGContextSetLineWidth(context, 10);
    //设置画笔颜色
    CGContextSetStrokeColorWithColor(context, [[UIColor colorWithRed:51/255.0 green:71/255.0 blue:95/255.0 alpha:0.5]CGColor]);
    //开始画路径
    CGContextStrokePath(context);
 
}


@end
